package com.wkhyapp.lm.view;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.lidong.photopicker.PhotoPickerActivity;
import com.lidong.photopicker.SelectModel;
import com.lidong.photopicker.intent.PhotoPickerIntent;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.constant.Constant;
import com.wkhyapp.lm.contract.AddXNPresenter;
import com.wkhyapp.lm.contract.AddXNView;
import com.wkhyapp.lm.http.net.CreateVirtualRequest;
import com.wkhyapp.lm.http.net.UpdateUserRequest;
import com.wkhyapp.lm.utils.IdUtils;
import com.wkhyapp.lm.utils.ImageLoadUtil;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.utils.StringUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AddXNSJActivity extends SuperActivity<AddXNPresenter> implements AddXNView {
    private static final int REQUEST_CAMERA_CODE = 10;
    private ArrayList<String> imagePaths = new ArrayList<>();
    private ArrayList<String> imgList = new ArrayList<>();
    @BindView(R.id.back_iv)
    ImageView back_iv;
    @BindView(R.id.save_rl)
    RelativeLayout save_rl;
    @BindView(R.id.head_iv)
    ImageView head_iv;
    private String headUrl;
    @BindView(R.id.sjmc_et)
    EditText sjmc_et;
    @BindView(R.id.dlsj_et)
    EditText dlsj_et;
    @BindView(R.id.ghzt_et)
    EditText ghzt_et;
    @BindView(R.id.bzxx_et)
    EditText bzxx_et;

    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);

        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M &&
                this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {

            this.requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }


    }

    @Override
    protected void initListener() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ghzt_et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //      要展示的数据
                final List<String> listData = new ArrayList<String>();
                listData.add("禁用");
                listData.add("普通账户");
                listData.add("供货商");
//      监听选中
                OptionsPickerView pvOptions = new OptionsPickerBuilder(AddXNSJActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        ghzt_et.setText(listData.get(options1));
                    }
                })
                        .setSelectOptions(0)//设置选择第一个
                        .setOutSideCancelable(false)//点击背的地方不消失
                        .build();//创建
                pvOptions.setPicker(listData);

                pvOptions.show();
            }
        });


        head_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoPickerIntent intent = new PhotoPickerIntent(AddXNSJActivity.this);
                intent.setSelectModel(SelectModel.MULTI);
                intent.setShowCarema(false); // 是否显示拍照
                intent.setMaxTotal(1); // 最多选择照片数量，默认为6
                intent.setSelectedPaths(imagePaths); // 已选中的照片地址， 用于回显选中状态
                startActivityForResult(intent, REQUEST_CAMERA_CODE);
            }
        });


        save_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(StringUtils.isEmpty(headUrl)){
                    TToast("请上传头像");
                    return;
                }
                if("".equals(sjmc_et.getText().toString())){
                    TToast("请输入商家名称");
                    return;
                }
                if("".equals(dlsj_et.getText().toString())){
                    TToast("请输入登陆手机");
                    return;
                }

                if("".equals(ghzt_et.getText().toString())){
                    TToast("请选择供货状态");
                    return;
                }

                if("".equals(bzxx_et.getText().toString())){
                    TToast("请输入备注信息");
                    return;
                }

                showLoad("正在保存");
                CreateVirtualRequest request = new CreateVirtualRequest();
                request.setHeadImg(headUrl);
                request.setNickname(sjmc_et.getText().toString());
                request.setPhone(dlsj_et.getText().toString());
                request.setDesc(bzxx_et.getText().toString());

                if("禁用".equals(ghzt_et.getText().toString())){
                    request.setStatus(0);
                }
                if("普通账户".equals(ghzt_et.getText().toString())){
                    request.setStatus(1);
                }
                if("供货商".equals(ghzt_et.getText().toString())){
                    request.setStatus(3);
                }
                mPresenter.upload(request);
            }
        });
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                // 选择照片
                case REQUEST_CAMERA_CODE:
                    ArrayList<String> list = data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT);
                    Log.d(TAG, "数量：" + list.size());
                    imgList.addAll(list);
                    if(list.size()>0){
                        showLoad("正在上传...");
                        mPresenter.getToken();
                    }
                    break;
            }
        }
    }

    @Override
    public void updateSucc() {
        dismissLoad();

        finish();
    }

    Configuration config = new Configuration.Builder().build();
    UploadManager uploadManager = new UploadManager(config);
    @Override
    public void setToken(String token) {
        dismissLoad();
        String key = "head_"+ MemberUtils.getMember()+"_"+System.currentTimeMillis()+"_"+ IdUtils.getShartUUID();
        uploadManager.put(imgList.get(0), key, token,
                new UpCompletionHandler() {
                    @Override
                    public void complete(String key, ResponseInfo info, JSONObject res) {
                        if(info.isOK()) {
                            headUrl = Constant.qiniuUrl+key;
                            ImageLoadUtil.setCirlcleImage_Normals(mContext,headUrl,head_iv);
                        }
                    }
                }, null);
    }

    @Override
    protected void afterCreate() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_xnsj;
    }

    @Override
    protected AddXNPresenter createPresenter() {
        return mPresenter = new AddXNPresenter(this);
    }


}
